﻿using d1Common;
using d1Common.Interface;
using d1Project.Modules.Common.do4A.Models;
using d1Project.Tools.JsonParse;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace d1Project.Modules.Common.do4A.Domain
{
    public class PermissionMgr
    {
        private static PermissionMgr singleInstance = new PermissionMgr();
        public static PermissionMgr Instance
        {
            get
            {
                return singleInstance;
            }
        }

        public PermissionMgr()
        {

        }

        public List<Permission> AllPermissions
        {
            get
            {
                return this.allPermissions;
            }
        }
        private List<Permission> allPermissions = new List<Permission>();

        public Dictionary<string, IDictionary<string, IRolePermission>> RolePermisssions
        {
            get
            {
                return this.rolePermisssions;
            }
        }
        private Dictionary<string, IDictionary<string, IRolePermission>> rolePermisssions = new Dictionary<string, IDictionary<string, IRolePermission>>();


        public Dictionary<string, Permission> DictPermissions
        {
            get
            {
                return this.dictPermissions;
            }
        }
        private Dictionary<string, Permission> dictPermissions = new Dictionary<string, Permission>();

        public void RegisterAllPermissions()
        {
            List<IDoModleService> _allModuleServices = doServices.Instance.GetAllModuleServices();
            foreach (IDoModleService _moduleService in _allModuleServices)
            {
                this.RegisterPermission(_moduleService.ModuleId);
            }
        }
        private void RegisterPermission(string _moduleID)
        {
            string _configContent = doServices.Instance.DoConfig.readConfigFile(_moduleID, "permission.json");
            if (_configContent == null) return;
            doJsonValue _jsonValueRoot = new Tools.JsonParse.doJsonValue();
            _jsonValueRoot.LoadDataFromText(_configContent);
            List<doJsonValue> _allJsonValues = _jsonValueRoot.GetArray();
            if (_allJsonValues == null) return;
            foreach (doJsonValue _configValue in _allJsonValues)
            {
                doJsonNode _jsonNodeConfig = _configValue.GetNode();
                if (_jsonNodeConfig == null) continue;
                Permission _newPermission = new Permission(_moduleID, _jsonNodeConfig);
                this.allPermissions.Add(_newPermission);
                this.dictPermissions[_newPermission.PermissionId] = _newPermission;

                if (_newPermission.Operations != null)
                {
                    foreach (Operation _opt in _newPermission.Operations)
                    {
                        PermissionOpt _newPermissionOpt = new PermissionOpt();
                        _newPermissionOpt.PermissionId = _newPermission.PermissionId;
                        _newPermissionOpt.PermissionName = _newPermission.PermissionName;
                        _newPermissionOpt.OperationName = _opt.Name;
                        doServices.Instance.DoCache.SetData(do4AModule.CacheGroupPermissonOpt, _newPermission.PermissionId + "_" + _opt.Id.ToString(), _newPermissionOpt);
                    }
                }
            }
        }

        public IDoPermissionOpt getDoPermissionOpt(string _permissionId, int _optId)
        {
            string _key = _permissionId + "_" + _optId.ToString();
            if (!doServices.Instance.DoCache.ContainsKey(do4AModule.CacheGroupPermissonOpt, _key)) return null;
            return doServices.Instance.DoCache.GetData<PermissionOpt>(do4AModule.CacheGroupPermissonOpt, _key);
        }

        public IDictionary<string, IRolePermission> GetRolePermissionsLocal(string _roleID)
        {
            do4ADbContext db = new do4ADbContext();
            try
            {
                IList<RolePermission> _permissions = db.RolePermissions.Where(x => (x.RoleId == _roleID)).ToList<RolePermission>();
                if (_permissions == null) return null;
                IDictionary<string, IRolePermission> _results = new Dictionary<string, IRolePermission>();
                foreach (RolePermission _rolePermission in _permissions)
                {
                    _results[_rolePermission.PermissionId] = _rolePermission;
                }
                return _results;
            }
            finally
            {
                db.Dispose();
            }
        }
    }
}